package com.fenghongzhang.day009test;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

/**
 * 内容提供者
 */
public class MyContentProvider extends ContentProvider {
    private SQLiteDatabase db;
    public MyContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int apple = db.delete("apple", selection, selectionArgs);
        return apple;
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        db.insert("apple",null,values);
        return uri;
    }

    @Override
    public boolean onCreate() {
        //创建数据库
        MySqlHelp mySqlHelp = new MySqlHelp(getContext(), "food.db", null, 1);
        db = mySqlHelp.getReadableDatabase();
        if(db != null){
            return true;
        }
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor apple = db.query("apple", projection, selection, selectionArgs, null, null, sortOrder);
        return apple;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int apple = db.update("apple", values, selection, selectionArgs);
        return apple;
    }
}
